// pages/card/card.js  
var app = getApp();
const innerAudioContext = wx.createInnerAudioContext();  //创建音频对象
innerAudioContext.autoplay = true;
innerAudioContext.loop = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    position:'',
    id:'',//当前选择的卡片
    musicStatus: true,  //音乐是否播放
    imgurl:'',
    hasphone:false,
  },
  indexHandle: function(options) {
    console.log('toindex');
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  editCardHandle: function (options) {
    if(!app.siteinfo.userinfo){
      wx.navigateTo({
        url: '/pages/welcome/welcome',
      })
    }else{
      wx.navigateTo({
        url: '/pages/editCard/editCard?id='+this.data.id,
      })
    }
   
  },
  // musicHandle
  musicHandle(e) {
    let that = this;
    let musicStatus = that.data.musicStatus;
    if (musicStatus) {
      innerAudioContext.pause();
      innerAudioContext.onPause(() => {
        console.log('暂停播放');
        that.setData({
          musicStatus: false
        })
      })
    } else {
      innerAudioContext.play();
      innerAudioContext.onPlay(() => {
        console.log('开始播放');
        that.setData({
          musicStatus: true
        })
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    if(app.siteinfo.userinfo){
      var phone = app.siteinfo.userinfo.phone || ''
      if(phone){
        that.setData({
          hasphone:true
        })
      }
    }
   
    that.setData({
      id: options.id
    })
  },
  // 获取手机号
  getPhoneNumber:function(e){
    var that = this;
    var phone = app.siteinfo.userinfo.phone
    console.log('hasphone')
    wx.checkSession({
      success: function () {
        
        var ency = encodeURIComponent(e.detail.encryptedData);
        var iv = e.detail.iv;
        var sessionk = wx.getStorageSync('sessionkey');

        if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
          
        } else {//同意授权
          wx.request({
            method: "GET",
            url: app.siteinfo.rootpath +'getphonenumber',
            data: {
              uid:app.siteinfo.userinfo.id,
              encryptedData: ency,
              iv: iv,
              sessionkey: sessionk
            },
            success: (res) => {
              console.log(res);
              var phone = res.data.phone;
              app.siteinfo.userinfo.phone = phone
              console.log(phone);
              that.setData({
                hasphone:true
              })
              wx.navigateTo({
                url: '/pages/editCard/editCard?id=' + that.data.id,
              })
            }, fail: function (res) {
              console.log("解密失败~~~~~~~~~~~~~");
              console.log(res);
            }
          });
        }
      },
      fail: function () {
        console.log("session_key 已经失效，需要重新执行登录流程");
        that.wxlogin(); //重新登录
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that  = this;
    wx.request({
      url: app.siteinfo.rootpath + 'gethk_one',
      method: "get",
      data: {
        id: that.data.id,
        action: 'hasmusic'
      },
      success: function (res) {
        // var str = res.data.greet.content.replace(/\n/g, "@#$");
        var content = res.data.greet.content.split("@#$");
        console.log(content)
        if (content[0]) {
          that.setData({
            greet: content,
          })
        } else {
          that.setData({
            greet: content[0],
          })
        }
        that.setData({
          imgurl: res.data.img,
          position: res.data.position,
        })
        innerAudioContext.src = res.data.music.content // 设置音频地址
        wx.setStorageSync('music', res.data.music)
        wx.setStorageSync('greet', res.data.greet)
        wx.setStorageSync('bgimg', res.data.img)
        wx.setStorageSync('position', res.data.position)
        innerAudioContext.onPlay(() => {
          console.log('开始播放')
        })
        innerAudioContext.onError((res) => {
          console.log(res.errMsg)
          console.log(res.errCode)
        })
        that.getHeight()
      }
    })
  },

  util: function (obj) {
    console.log(obj);
    console.log(-obj.list);
    var continueTime = (parseInt(obj.list / obj.container) + 1) * 8000;
    var setIntervalTime = 200 + continueTime;

    var animation = wx.createAnimation({
      duration: 200,  //动画时长
      timingFunction: "linear", //线性
      delay: 0  //0则不延迟
    });
    this.animation = animation;
    animation.translateY(obj.container).step({ duration: 200, timingFunction: 'step-start' }).translateY(-obj.list + 10).step({ duration: continueTime });
    this.setData({
      animationData: animation.export()
    })
    setInterval(() => {
      animation.translateY(obj.container).step({ duration: 200, timingFunction: 'step-start' }).translateY(-obj.list + 10).step({ duration: continueTime });
      this.setData({
        animationData: animation.export()
      })
    }, setIntervalTime)
  },
  getHeight() {
    var obj = new Object();
    //创建节点选择器
    var query = wx.createSelectorQuery();
    query.select('.greeting').boundingClientRect()
    query.select('.text').boundingClientRect()
    query.exec((res) => {
      obj.container = res[0].height; // 框的height
      obj.list = res[1].height; // list的height
      // return obj;
      console.log(obj.container)
      console.log(obj.list)
      this.util(obj);
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    innerAudioContext.stop()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    innerAudioContext.stop()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})